package com.demo.mantis.web;

import javax.inject.Singleton;

import cn.techarts.dragonfly.web.Controller;
import cn.techarts.dragonfly.web.WebContext;
import cn.techarts.dragonfly.web.rest.Get;


@Singleton
@Controller
public class CalculatorWebService {
	
	@Get("/plus/{x}/{y}") 
	public float plus(WebContext arg) {
		var x = arg.floatAt(0);
		var y = arg.floatAt(1);
		return x + y;		
	}
	
	@Get("/subtract/{x}/{y}")
	public float subtract(WebContext arg){
		var x = arg.floatAt(0);
		var y = arg.floatAt(1);
		return x - y;	
	}
	
	@Get("/multiply/{x}/{y}")
	public float multiply(WebContext arg) {
		var x = arg.floatAt(0);
		var y = arg.floatAt(1);
		return x * y;		
	}
	
	@Get("/divide/{x}/{y}")
	public float divide(WebContext arg) {
		var x = arg.floatAt(0);
		var y = arg.floatAt(1);
		if(y == 0f) {
			arg.error(-1, "Divide by zero.");
			return 0;			
		}
		else {
			return x / y;
		}
		
	}


}




